package it.uniroma3.dia.praised.core.datasource;


import it.uniroma3.dia.praised.core.exception.PersistenceException;
import it.uniroma3.dia.praised.core.model.ProteinName;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class DAOProteinName {

	public static final String GET_PROTEINNAMES_DISTINCT = "select distinct fullname from proteinnames order by fullname";
	
	/**
	 * 
	 * @return
	 * @throws PersistenceException
	 */
	public static List<ProteinName> doRetrieveAll() throws PersistenceException{
		DataSource ds = DataSource.getInstance();
		Connection con = ds.getConnection();
		PreparedStatement statement = null;
		ResultSet result = null;
		List<ProteinName> proteins = null ;
				
		try{
			String query = GET_PROTEINNAMES_DISTINCT;
			statement = con.prepareStatement(query);
			result = statement.executeQuery();
			
			if(result!= null){
				proteins = new LinkedList<ProteinName>();
				while(result.next()){
					ProteinName protein = new ProteinName();
					protein.setFullName(result.getString("fullName"));
					
					proteins.add(protein);
				}
			}
		} catch(SQLException e) {
			throw new PersistenceException(e.getMessage());
			
		} finally {
			try {
				if(result!=null)
					result.close();
				if(statement!=null)
					statement.close();
				if(con!=null)
					con.close();
			} catch(SQLException e) {
				throw new PersistenceException(e.getMessage());
			}
		}
		return proteins;
	}
}
